package student;

import java.awt.EventQueue;
public class ClockTimer {

	public JFrame frame;
	private final JLabel hh = new JLabel("0");
	public JLabel mm = new JLabel("0");
	public JLabel ss = new JLabel("0");
	public int diff_h;
	public int diff_m;
	public int diff_s;
	public JButton btnStart;
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClockTimer window = new ClockTimer();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	public Timer t;
	
	public ClockTimer() {
		initialize();
		t = new Timer(1000, new ActionListener(){
            public void actionPerformed(ActionEvent e){
            	int th = 16;
            	int tm = 55;
            	int ts = 60;
            	Calendar calendar = new GregorianCalendar();
        		int hour = calendar.get(Calendar.HOUR);
        		int minute = calendar.get(Calendar.MINUTE);
        		int second = calendar.get(Calendar.SECOND);
        		
        		if(calendar.get(Calendar.AM_PM) == 1)
        		{
        			hour = hour + 12;
        		}
            	diff_h = th - hour;
            	diff_m = tm - minute;
            	diff_s = ts - second;
            	if(diff_h < 0 || diff_m < -10 )
                {
                	                	
        			JOptionPane.showMessageDialog(null, " You are more than 10 min late \n\n You are not allowed to give the test paper \n\n","Login Fail", JOptionPane.ERROR_MESSAGE);
                   	t.stop();
                   	                  	                	
                }
            	else if (diff_h == 0 && diff_m < 0 && diff_m >= -10)
            	{
            		JOptionPane.showMessageDialog(null, " You are late !!! ","HURRY UP", JOptionPane.PLAIN_MESSAGE);
            		btnStart.setVisible(true);
            		t.stop();
                   	
            	}
            	else
            	{
            		String dh,dm,ds;
            		if(diff_h < 0 && diff_m < 0 )
            		{
            			diff_h = 0;
            			diff_m = 0;
            		}
            		if(diff_h < 0)
            		{
            			hh.setText("00");
            		}
            		else
            		{
	            	   dh = Integer.toString(diff_h);
		            	hh.setText(dh);
            		}
	            	 dm = Integer.toString(diff_m);
	            	mm.setText(dm);
	            	ds = Integer.toString(diff_s);
	            	ss.setText(ds);
	                int hr = Integer.parseInt(hh.getText());
	                int min = Integer.parseInt(mm.getText());
	                int sec = Integer.parseInt(ss.getText());
	                sec--;
	                if(sec==0)
	                {
	                    sec=60%60;
	                    min=min-1;
	                    if(min < 0)
	                    {
	                    	min = 0;
	                    }
	                    if(min == 0)
	                    {
	                        
	                    	min = 60%60;
	                        hr--;
	                        if(hr == 0)
	                        {
	                            hr = 24;
	                        }
	                        if(hr <= 0)
	                        {
	                            hr = 0;
	                        }
	                    }
	                }
	                hh.setText(""+hr);
	                mm.setText(""+min);
	                ss.setText(""+sec);
	                
	                if(diff_h == 0 && diff_m == 0 && diff_s == 1)
	                {
	                	
	                	t.stop();
	                   	btnStart.setVisible(true);
	                   	                	
	                }
            	}   	
            }
        }
        );
       t.start();
		
	
	}

	
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(410, 210, 478, 300);
		frame.getContentPane().setLayout(null);
		
		hh.setFont(new Font("Tahoma", Font.BOLD, 46));
		hh.setHorizontalAlignment(SwingConstants.CENTER);
		hh.setBounds(43, 114, 81, 53);
		frame.getContentPane().add(hh);
		
		JLabel lblNewLabel_1 = new JLabel(":");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 48));
		lblNewLabel_1.setBounds(123, 114, 46, 53);
		frame.getContentPane().add(lblNewLabel_1);
		
	
		mm.setFont(new Font("Tahoma", Font.BOLD, 46));
		mm.setHorizontalAlignment(SwingConstants.CENTER);
		mm.setBounds(190, 114, 81, 53);
		frame.getContentPane().add(mm);
		
		JLabel lblNewLabel_3 = new JLabel(":");
		lblNewLabel_3.setFont(new Font("Tahoma", Font.BOLD, 48));
		lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel_3.setBounds(281, 114, 46, 53);
		frame.getContentPane().add(lblNewLabel_3);
		
	
		ss.setFont(new Font("Tahoma", Font.BOLD, 46));
		ss.setHorizontalAlignment(SwingConstants.CENTER);
		ss.setBounds(351, 114, 81, 53);
		frame.getContentPane().add(ss);
		
		JLabel lblNewLabel_5 = new JLabel("Time to start paper");
		lblNewLabel_5.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblNewLabel_5.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel_5.setBounds(10, 35, 442, 38);
		frame.getContentPane().add(lblNewLabel_5);
		
		 btnStart = new JButton("Start");
		btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });
		btnStart.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnStart.setBounds(190, 228, 89, 23);
		frame.getContentPane().add(btnStart);
		btnStart.setVisible(false);
		
		if(diff_h == 0 && diff_m == 0 && diff_s == 2)
		{
			this.frame.setVisible(false);
		}
	}
	
	private void btnStartActionPerformed(java.awt.event.ActionEvent evt)
	{
		
		this.frame.setVisible(false);
		TestPaper tp = new TestPaper();
    	tp.frame.setVisible(true);
		
	}
	
}
